<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    :close-on-click-modal="false"
    width="600px"
    @close="close"
  >
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" size="small" label-width="130px">
      <el-form-item label="会员：">
        {{ vipDetail.member.name||vipDetail.customer.nickName||'-' }}
      </el-form-item>
      <el-form-item label="当前有效期：">
        {{ vipDetail.member.expireTime }}
      </el-form-item>
      <el-form-item label="调整后有效期：" prop="expireTime">
        <el-date-picker
          v-model="form.expireTime"
          :picker-options="pickerOptions"
          type="datetime"
          default-time="23:59:59"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择有效期"
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { renewMember } from '@/api/vip'
export default {
  props: {
    vipDetail: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      // 校验字段
      rules: {
        expireTime: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ]
      },
      // 配置
      title: '调整会员有效期',
      resolve: null,
      reject: null,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now()
        }
      },
      // 状态
      dialogVisible: false,
      loading: false,
      // 数据
      form: {
        memberId: null,
        expireTime: null
      }
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {

  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    // 弹框相关
    open() {
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    close() {
      this.form = {
        memberId: null,
        expireTime: null
      }
      this.$refs.form.resetFields()
      this.loading = false
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.form.memberId = this.vipDetail.member.id
          renewMember(this.form).then(() => {
            this.$notify({
              title: '成功',
              message: '调整成功',
              type: 'success',
              duration: 2000,
              offset: 40
            })
            this.resolve && this.resolve()
            this.hide()
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }

    /**
     * 数据组
     */
    /**
     * 按钮组
     */
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
